package com.example.xmpic.ui.adapter;

import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.xmpic.R;
import com.example.xmpic.R2;
import com.example.xmpic.ui.data.CameraLocalFile;
import com.example.xmpic.ui.utils.OnLocalFileItemClick;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：摄像头本地录像适配器
 */
public class LocalVideoAdapter extends RecyclerView.Adapter<LocalVideoAdapter.ItemHolder> {

    private Context context;
    private List<CameraLocalFile> mList;
    private OnLocalFileItemClick mCallBack;

    public LocalVideoAdapter(Context context, OnLocalFileItemClick callBack) {
        this.context = context;
        this.mCallBack = callBack;
    }

    public void setLists(List<CameraLocalFile> lists) {
        this.mList = lists;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_device_camera_pic, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder viewHolder, int i) {
        CameraLocalFile video = mList.get(i);
        Glide.with( context ).load(Uri.fromFile(new File(video.getFilepath()))).into(viewHolder.imageView);
        viewHolder.textView_name.setText(video.getFilename());
        viewHolder.textView_time.setText(video.getModifyTime());
        viewHolder.itemView.setOnClickListener(view -> mCallBack.onItemClick(video));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.iv_push_result_checked)
        ImageView imageView;
        @BindView(R2.id.tv_push_result_event)
        TextView textView_name;
        @BindView(R2.id.tv_push_result_time)
        TextView textView_time;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}